<extend name="layout/edit" />

<block name="HEADER">

</block>

<block name="CONTENT">
    <div style="height: calc(100% - 42px);">
    	<control:grid id="giftbagGrid" />
	</div>
    <div class="xw-form">
		<div class="xw-btnRow" style="text-align: right;">
			<button type="button" class="layui-btn layui-btn-normal layui-btn-sm" onclick="page.save()" >
				确定
			</button> 
			<button type="button" class="layui-btn layui-btn-normal layui-btn-sm" onclick="$.close()">
				关闭
			</button>
		</div>
	</div>
</block>

<block name="FOOTER">
   <script type="text/javascript">
    var  page=(function(ns){
    	
		ns.init = function(){
			if(parent.__gift_bag){
				giftbagGrid.addRow(parent.__gift_bag)
			}else{
				giftbagGrid.load([{"field":"specs_id","opera":"=","value": $.getRequest('specs_id') }]);
			}
		}
		
		//赠送内容添加行
		ns.addRow = function(){
			giftbagGrid.addRow([{
				name:'',
				integral:0,
				coupon_id:'',
                coupon_name:'',
				coupon_num:'',
				gift_name:'',
				gift_num:'',
			}]);
		}
		//规格删除行
		ns.deleteRow = function(){
    		var selectedRow = giftbagGrid.getSelectedRows()[0];
			giftbagGrid.deleteRow();   
		}
		
		//保存
		ns.save = function(){
			if(!giftbagGrid.verification()){
				return false;
			}
			parent.page.giftBagCallback( giftbagGrid.getRows() );
			$.close();
		}

        //选择优惠券
        ns.selectCoupon = function(){
            var filter = {
                url:"modules/common/selectCoupon",
                mode:1,
                ids: $.map(giftbagGrid.getRows(),function(h){
                	if(h.coupon_id) return h.coupon_id;
            	}).join(',')
            }
            $.openIframe(filter,"选择优惠券",['500px', '380px']);
        }
        ns.selectCouponCallback = function(ids,data){
            var selectedRow = giftbagGrid.getSelectedRows()[0];
			giftbagGrid.update(selectedRow.LAY_TABLE_INDEX,{
                coupon_id:ids,
			    coupon_name:data[0]['name']
			});
        }
		
        return ns;
    }({}));
    
    $(function(){ page.init(); });
   </script>
</block>



